package com.nmgydy.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class QrcodeUtil {

	public static byte[] getQrcode(String jumpUrl){
		//输出
		String format = "png";// 图像类型
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		hints.put(EncodeHintType.MARGIN, 0);
		BitMatrix bitMatrix = null;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		// 生成矩阵
		//String code = "http://demo.nmgydy.com/cosmetic-hospital/user/accept_discount.html";
		String code = jumpUrl;
		try {
			// 输出图像
			bitMatrix = new MultiFormatWriter().encode(code,BarcodeFormat.QR_CODE, 104, 104, hints);
			//FileOutputStream ostream = new FileOutputStream(qrcodeFile);
			MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream);
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outputStream.toByteArray();
	}
	public static void main(String[] args) {
		getQrcode("123");
	}
}
